package org.springside.examples.showcase.wb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
import org.springside.examples.showcase.wb.entity.WbContent;
import org.springside.examples.showcase.wb.entity.WbContentRet;
import org.springside.modules.orm.Page;

@Component
public class WbContentJdbcDao {
	private static final String HOTFEEDS = "SELECT R.RET_MID,R.COUNT,R.PARDATE,W.* FROM RET_TOP R INNER JOIN SINA_WB W ON W.IR_MID=R.RET_MID WHERE R.PARDATE = ? ORDER BY R.COUNT DESC LIMIT ?, ?";
	private static final String NEEDDOWNLOAD = "select distinct rt.ret_mid,wb.ir_retweeted_uid,wb2.ir_mid from ret_top rt left join sina_wb wb on rt.ret_mid = wb.ir_retweeted_mid left join sina_wb wb2 on rt.ret_mid = wb2.ir_mid where rt.pardate=? and wb2.ir_mid is null ";
	private JdbcTemplate jdbcTemplate;
	private TransactionTemplate transactionTemplate;

	@Resource
	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	@Resource
	public void setDefaultTransactionManager(PlatformTransactionManager defaultTransactionManager) {
		transactionTemplate = new TransactionTemplate(defaultTransactionManager);
	}

	private WbMapper wbMapper = new WbMapper();

	private class WbMapper implements RowMapper<WbContentRet> {
		public WbContentRet mapRow(ResultSet rs, int rowNum) throws SQLException {
			WbContentRet wb = new WbContentRet();
			wb.setIr_mid(rs.getLong("ir_mid"));
			wb.setIr_uid(rs.getString("ir_uid"));
			wb.setIr_screen_name(rs.getString("ir_screen_name"));
			wb.setIr_status_content(rs.getString("ir_status_content"));
			wb.setIr_created_at(rs.getDate("ir_created_at"));
			wb.setIr_thumbnail_pic(rs.getString("ir_thumbnail_pic"));
			wb.setIr_urlname(rs.getString("ir_urlname"));
			wb.setIr_rttcount(rs.getInt("ir_rttcount"));
			wb.setIr_commtcount(rs.getInt("ir_commtcount"));
			wb.setCount(rs.getInt("count"));
			wb.setRet_mid(rs.getLong("ret_mid"));
			wb.setPardate(rs.getString("pardate"));
			return wb;
		}
	}

	public List<WbContentRet> getHotRetWbs(Page<WbContentRet> page, String pardate) {
		List<WbContentRet> result = jdbcTemplate.query(HOTFEEDS, wbMapper, pardate, (page.getPageNo() - 1) * page.getPageSize(), page.getPageSize());
		return result;
	}

	public List<WbContent> getNeedDownloadFeeds(String whichDay) {
		List<WbContent> result = jdbcTemplate.query(NEEDDOWNLOAD, new RowMapper<WbContent>() {
			@Override
			public WbContent mapRow(ResultSet rs, int rowNum) throws SQLException {
				WbContent wb = new WbContent();
				wb.setIr_mid(rs.getLong("ret_mid"));
				wb.setIr_uid(rs.getString("ir_retweeted_uid"));
				return wb;
			}
		}, whichDay);
		return result;
	}

	public List<WbContentRet> selects(String sql, RowMapper<WbContentRet> mapper, Object... args) {
		List<WbContentRet> result = jdbcTemplate.query(sql, mapper, args);
		return result;
	}
}
